package com.ruoyi.web.controller.contest;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.contest.mapper.TStudentMapper;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.contest.domain.TStudent;
import com.ruoyi.contest.service.ITStudentService;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 *
 * @author ruoyi
 * @date 2024-03-01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/contest/student")
public class TStudentController extends BaseController
{
    @Resource
    private final ITStudentService tStudentService;
    @Resource
    private final TStudentMapper tStudentMapper;

    @Autowired
    private final ISysDeptService deptService;


    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('contest:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStudent tStudent)
    {
        startPage();
        List<TStudent> list = tStudentService.selectTStudentList(tStudent);
        for(TStudent tStudent1 : list){
            SysDept sysDept = deptService.selectDeptById(tStudent1.getDeptId());
            tStudent1.setDeptName(sysDept.getDeptName());
        }

        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('contest:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TStudent tStudent)
    {
        List<TStudent> list = tStudentService.selectTStudentList(tStudent);
        ExcelUtil<TStudent> util = new ExcelUtil<TStudent>(TStudent.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('contest:student:query')")
    @GetMapping(value = "/{stuId}")
    public AjaxResult getInfo(@PathVariable("stuId") Integer stuId)
    {
        return success(tStudentMapper.getById(stuId));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('contest:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStudent tStudent)
    {
        if(tStudent != null){
            return toAjax(tStudentService.save(tStudent));
        }
        return error();
    }



    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('contest:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStudent tStudent)
    {
        return toAjax(tStudentService.updateById(tStudent));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('contest:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stuIds}")
    public AjaxResult remove(@PathVariable Long[] stuIds)
    {
        return toAjax(tStudentService.removeByIds(Arrays.asList(stuIds)));
    }
}